package edu.bit.water.controller;

import edu.bit.water.dto.WarningRule;
import edu.bit.water.service.WarningRuleService;
import edu.bit.water.util.ListToSqlString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/warning_rule")
public class WarningRuleController {
    @Autowired
    private WarningRuleService warningRuleService;


    /**
     * @Description: 查询watersensor
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryWarningRule() {
        Map<String, Object> modelMap = new HashMap<>();
        List<WarningRule> list = warningRuleService.queryWarningRule();
        modelMap.put("rows", list);
        return modelMap;
    }

    @RequestMapping(value = "/select_by_company_id", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryWarningRule(String company_id) {
        Map<String, Object> modelMap = new HashMap<>();
        List<WarningRule> list = warningRuleService.queryWarningRuleByCompanyId(company_id);
        modelMap.put("rows", list);
        return modelMap;
    }



    /**
     * @Description: 批量删除watersensor, 默认删除成功
     * @Param: 删除的设备号list
     * @return: Map
     * @Author: YYF
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> ids) {
        //System.out.println(ids);
        StringBuilder temp = new StringBuilder(ids.toString());
        temp.setCharAt(0, '(');
        temp.setCharAt(temp.length() - 1, ')');
        warningRuleService.deleteWarningRule(new String(temp));

        Map<String, String> modelMap = new HashMap<>();
        modelMap.put("status", "success");
        return modelMap;
    }

    /**
     * @Description: 插入
     * @Param: WarningRule
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, Object> insertWarningRule(WarningRule warningRule) {
        if (warningRule.getDefination() == null) {
            warningRule.setDefination("-");
        }
        //后台设置创建时间
        warningRule.setCreated_At();
        return warningRuleService.insertWarningRule(warningRule);
    }

    /**
     * @Description: 更新
     * @Param: WarningRule
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateWarningRule(WarningRule warningRule) {
        return warningRuleService.updateWarningRule(warningRule);
    }

    /**
     * @Description:查询预警规则
     * @Param： name&createDate
     * @return: Map
     * @Author: wlj
     */
    @ResponseBody
    @RequestMapping(value = "/queryrulebynameandcreatedate")
    public Map<String, Object> queryRuleByNameAndCreateDate(@RequestParam(required = false) List<String> name, @RequestParam(required = false) List<String> createdate) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (name == null || createdate == null) {
            map.put("defeat", "defeat");
            return map;
        }
        return warningRuleService.queryRuleByNameAndCreateDate(ListToSqlString.toString(name), ListToSqlString.toString(createdate));
    }

    /**
     * 根据所在位置（车间）查询设备类型
     */
    @ResponseBody
    @RequestMapping(value="/querySensorTypeByLocation")
    public Map<String, Object> querySensorTypeByLocation(@RequestParam("businessId") String businessId,
                                                         @RequestParam("locations") String locations) {
        return warningRuleService.querySensorTypeByLocation(businessId, locations);
    }
}
